package com.bea.ftd.dashboard.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bea.ftd.common.ValidationException;
import com.bea.ftd.common.dao.ApiDefineRepo;
import com.bea.ftd.common.entity.ApiDefine;
import com.bea.ftd.common.enums.ValidationErrorCode;
import com.bea.ftd.dashboard.service.IApiDefineService;
import com.bea.ftd.dashboard.validator.BooleanValidator;
import com.bea.ftd.dashboard.validator.NumberValidator;
import com.bea.ftd.dashboard.validator.StringValidator;

@Service
public class ApiDefineServiceImpl implements IApiDefineService {
	private Logger LOGGER = LoggerFactory.getLogger(ApiDefineServiceImpl.class);

	@Autowired
	private ApiDefineRepo apiDefineRepo;
	@Autowired
	private StringValidator stringValidator;
	@Autowired
	private NumberValidator numberValidator;
	@Autowired
	private BooleanValidator booleanValidator;

	@Override
	public void addApiDefine(ApiDefine apiDefine) throws ValidationException {

		setCreateOrUpdateTime(apiDefine);
		validateInputParams(apiDefine);
		apiDefineRepo.save(apiDefine);

	}

	private void setCreateOrUpdateTime(ApiDefine apiDefine) {
		Optional<ApiDefine> opt = apiDefineRepo.findById(apiDefine.getId());
		if (!opt.isPresent()) {
			apiDefine.setCreateTime(Calendar.getInstance().getTime());
		} else {
			apiDefine.setCreateTime(opt.get().getCreateTime());
		}

		apiDefine.setUpdateTime(Calendar.getInstance().getTime());
	}

	private void validateInputParams(ApiDefine apiDefine) throws ValidationException {
		if (stringValidator.isEmptyString(apiDefine.getId())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "id不能为空");
		}

		if (stringValidator.isEmptyString(apiDefine.getParnterCode())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "商户编号不能为空");
		}

		if (stringValidator.isEmptyString(apiDefine.getPath())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "路径不能为空");
		}

		if (stringValidator.isEmptyString(apiDefine.getServiceId())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "服务id不能为空");
		}

		if (stringValidator.isEmptyString(apiDefine.getUrl())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "url不能为空");
		}

		if (!numberValidator.isNonNegtiveNumber(apiDefine.getRetryable())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "重试次数应该大于等于0");
		}

		if (!booleanValidator.isBooleanType(apiDefine.getEnabled())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "无效的enabled值");
		}

		if (!numberValidator.isNonNegtiveNumber(apiDefine.getStripPrefix())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "重试次数应该大于等于0");
		}

		if (stringValidator.isEmptyString(apiDefine.getApiName())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "api名不能为空");
		}

		if (stringValidator.isEmptyString(apiDefine.getFallbackUrl())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "回退地址不能为空");
		}
	}

	@Override
	public List<ApiDefine> getAllApiDefines() {
		try {
			return apiDefineRepo.findAll();
		} catch (Exception e) {
			LOGGER.error("error when querying apiDefines from database " + e.getMessage());
			return null;
		}
	}

	@Override
	public List<String> getAllApiDefineIds() {
		return apiDefineRepo.getAllApiDefineIds();
	}
}
